"""
11
09:41
18:06
12:14
00:59
00:00
14:34
01:01
19:07
11:59
12:00
21:37


09:41 AM
06:06 PM
12:14 PM
12:59 AM
12:00 AM
02:34 PM
01:01 AM
07:07 PM
11:59 AM
12:00 PM
09:37 PM

"""

n = int(input())

for i in range(n):
    a, b = map(int, input().split(':'))
    if a > 12:
        print("%02d:%02d PM" % (a - 12, b))
    elif a == 12:
        print("%02d:%02d PM" % (a, b))
    elif a == 0:
        print("%02d:%02d AM" % (12, b))
    else:
        print("%02d:%02d AM" % (a, b))
